﻿using System;
using System.Globalization;
using iTextSharp.text;
using iTextSharp.text.pdf;

namespace Common.Modules
{
    /// <summary>
    /// Pdf文档生成的事件类，用于生成页眉页脚
    /// </summary>
    public class PdfEvent : PdfPageEventHelper
    {
        private string _institution, _no, _name, _gender;

        private int _age;

        private Font _font = FontFactory.GetFont(
                "STSONG", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED, 10, Font.NORMAL, BaseColor.BLACK);

        private Font _headerFont;

        PdfTemplate _total;

        public void SetHeader(string institution, string no, string name, string gender)
        {
            _institution = institution;
            _no = no;
            _name = name;
            _gender = gender;
        }

        public override void OnOpenDocument(PdfWriter writer, Document document)
        {
            _total = writer.DirectContent.CreateTemplate(30, 16);
        }

        public override void OnEndPage(PdfWriter writer, Document document)
        {
            // 首页不设置页眉页脚
            if (writer.PageNumber == 1) return;

            try
            {
                var table = new PdfPTable(3);
                table.SetWidths(new[] { 24f, 24f, 2f });
                table.TotalWidth = document.Right - document.Left;
                table.LockedWidth = true;
                table.DefaultCell.FixedHeight = 20;
                table.DefaultCell.Border = Rectangle.TOP_BORDER;
                table.AddCell(new Phrase(_institution, _font));

                table.DefaultCell.HorizontalAlignment = Element.ALIGN_RIGHT;
                table.AddCell(new Phrase(String.Format("第 {0} 页，共 ", writer.PageNumber - 1), _font));

                var cell = new PdfPCell(Image.GetInstance(_total)) { Border = Rectangle.TOP_BORDER };
                table.AddCell(cell);

                // write to footer
                table.WriteSelectedRows(0, -1, 50, 50, writer.DirectContent);
            }
            catch (DocumentException ex)
            {
                throw new Exception("生成页码错误，详细信息：" + ex.Message);
            }
        }

        public override void OnCloseDocument(PdfWriter writer, Document document)
        {
            ColumnText.ShowTextAligned(_total, Element.ALIGN_LEFT, new Phrase((writer.PageNumber - 2) + " 页", _font), 2, 4, 0);
        }

        public override void OnStartPage(PdfWriter writer, Document document)
        {
            // 首页不设置页眉页脚
            if (writer.PageNumber == 1) return;

            try
            {
                var vs = new[] { "医保编码：", _no, "姓名：", _name, "性别：", _gender, "" };
                var table = new PdfPTable(vs.Length);
                table.SetWidths(new[] { 27f, 60f, 18f, 30f, 18f, 22f, 20f });
                table.TotalWidth = document.Right - document.Left;
                table.LockedWidth = true;
                table.DefaultCell.FixedHeight = 20;
                for (var i = 0; i < vs.Length; i++)
                {
                    var cell = new PdfPCell(new Phrase(vs[i], _font))
                                   {
                                       Border = Rectangle.BOTTOM_BORDER,
                                       PaddingBottom = 5,
                                       VerticalAlignment = i % 2 == 0 ? Element.ALIGN_RIGHT : Element.ALIGN_LEFT
                                   };
                    table.AddCell(cell);
                }
                table.WriteSelectedRows(0, -1, document.Left, document.Top - document.Bottom + 70, writer.DirectContent);
            }
            catch (DocumentException ex)
            {
                throw new Exception("生成页眉错误，详细信息：" + ex.Message);
            }
        }

    }

}
